package com.thz.admin.controller.user;

import com.thz.common.dto.ConversionRecordDTO;
import com.thz.common.dto.user.UserInfoDTO;
import com.thz.common.result.Result;
import com.thz.common.utils.IpUtils;
import com.thz.system.service.user.IUserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 用户信息控制器
 * 
 * @author THZ
 * @date 2025
 */
@Slf4j
@RestController
@RequestMapping("/api/user")
@Tag(name = "用户管理", description = "基于IP的用户信息管理")
public class UserInfoController {
    
    @Autowired
    private IUserInfoService userInfoService;
    
    /**
     * 获取当前用户信息
     * 自动根据IP创建或获取用户
     */
    @GetMapping("/current")
    @Operation(summary = "获取当前用户信息", description = "根据IP地址获取或创建用户信息")
    public Result<UserInfoDTO> getCurrentUser(HttpServletRequest request) {
        String ipAddress = IpUtils.getClientIp(request);
        log.info("获取用户信息: IP={}", ipAddress);
        
        UserInfoDTO userInfo = userInfoService.getOrCreateUser(ipAddress);
        return Result.success(userInfo);
    }
    
    /**
     * 获取用户的转换记录历史（统一接口，包含所有类型的转换）
     */
    @GetMapping("/history")
    @Operation(summary = "获取转换历史", description = "获取当前用户的所有类型转换记录历史（视频转GIF、文件转SVG等）")
    public Result<List<ConversionRecordDTO>> getConversionHistory(
            HttpServletRequest request,
            @RequestParam(value = "limit", required = false, defaultValue = "7") int limit) {
        String ipAddress = IpUtils.getClientIp(request);
        log.info("查询用户转换历史: IP={}, limit={}", ipAddress, limit);
        
        List<ConversionRecordDTO> history = userInfoService.getUserConversionHistory(ipAddress, limit);
        return Result.success(history);
    }
    
    /**
     * 删除用户的转换记录
     */
    @DeleteMapping("/history/{recordId}")
    @Operation(summary = "删除转换记录", description = "删除当前用户的转换记录")
    public Result<Void> deleteConversion(
            HttpServletRequest request,
            @PathVariable Long recordId) {
        String ipAddress = IpUtils.getClientIp(request);
        log.info("删除用户转换记录: IP={}, RecordId={}", ipAddress, recordId);
        
        userInfoService.deleteUserConversion(ipAddress, recordId);
        return Result.success("删除成功", null);
    }
    
    /**
     * 上传用户头像
     */
    @PostMapping("/avatar")
    @Operation(summary = "上传头像", description = "上传用户头像图片")
    public Result<UserInfoDTO> uploadAvatar(
            HttpServletRequest request,
            @RequestParam("avatar") MultipartFile avatarFile) {
        String ipAddress = IpUtils.getClientIp(request);
        log.info("用户上传头像: IP={}, 文件名={}, 大小={}", 
                ipAddress, avatarFile.getOriginalFilename(), avatarFile.getSize());
        
        UserInfoDTO userInfo = userInfoService.uploadAvatar(ipAddress, avatarFile);
        return Result.success("头像上传成功", userInfo);
    }
}

